package com.mytx.sso.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.mytx.sso.config.AliYunConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 发送短信的工具类
 */
@Component
public class SendSmsUtils {
    private static final Logger logger = LoggerFactory.getLogger(SendSmsUtils.class);

    @Autowired
    private IAcsClient client;
    @Autowired
    private AliYunConfig aliYunConfig;

    public CommonResponse sendSms(String phoneNumber, String code){

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain(aliYunConfig.domain);
        request.setVersion(aliYunConfig.VERSION);
        request.setAction(aliYunConfig.SEND_SMS_ACTION);
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", aliYunConfig.SIGNATURE);
        request.putQueryParameter("TemplateCode", aliYunConfig.TEMPLATE_CODE);
        request.putQueryParameter("TemplateParam", code);
        try {
            return client.getCommonResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            logger.error("SendSmsUtils-->sendSms:短信发送失败:phone:{},code:{}",phoneNumber,code);
            return null;
        }
    }
}
